package com.dd.machine.main.user;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.dd.machine.R;
import com.dd.machine.base.activity.BaseActivity;
import com.dd.machine.base.utils.ToastUtil;
import com.dd.machine.main.commons.CountDownTimerUtils;
import com.dd.machine.main.commons.SPUtils;
import com.dd.machine.main.desktop.DeskTopActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.dd.machine.base.net.URLManager.MOBILEBIND;
import static com.dd.machine.base.utils.StringUtils.isMobile;
import static com.dd.machine.main.commons.PostServer.getVerifyCode;

/**
 * Created by Administrator on 2017/7/18.
 */

public class BindPhoneActivity extends BaseActivity {
    @Bind(R.id.back)
    RelativeLayout back;
    @Bind(R.id.phoneNum_et)
    EditText phoneNumEt;
    @Bind(R.id.code_et)
    EditText codeEt;
    @Bind(R.id.getCodeTv)
    TextView getCodeTv;
    @Bind(R.id.bind_btn)
    Button bindBtn;


    boolean ret = false;
    private String phoneNum;
    private String myPhoneNum;
    private String openId="";
    private String platform = "";
    Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_bind);
        ButterKnife.bind(this);
        bundle = getIntent().getExtras();
        openId = bundle.getString("openId");
        platform = bundle.getString("platform");
    }

    @OnClick({R.id.back, R.id.getCodeTv, R.id.bind_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                Intent intent = new Intent(BindPhoneActivity.this, DeskTopActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.back_anim, R.anim.backback_anim);
                break;
            case R.id.getCodeTv:
                getCode();
                break;
            case R.id.bind_btn:
                bindTo();
                break;
        }
    }

    private void getCode(){
        phoneNum = phoneNumEt.getText().toString();
        if (phoneNum.isEmpty()) {
            ToastUtil.showShortToast(this,"请输入手机号");
            return;
        }
        if (isMobile(phoneNum)) {
            CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(getCodeTv, 60000, 1000);
            mCountDownTimerUtils.start();
            getCodeTv.setTextColor(0xff666666);
            ret = true;//判断已经点击获取验证码
            myPhoneNum = phoneNum;
            getVerifyCode(phoneNum);

        }else {
            ToastUtil.showShortToast(this,"请正确输入手机号");
        }
    }

    private void bindTo(){
        String numCode = codeEt.getText().toString();
        if (ret) {
            if (numCode.isEmpty()) {
                ToastUtil.showShortToast(BindPhoneActivity.this,"请输入验证码");
                return;
            }

            bindPhone(myPhoneNum,numCode,openId,platform);
        }else {
            ToastUtil.showShortToast(BindPhoneActivity.this,"请点击获取验证码");
        }
    }

    private void bindPhone(String mobile,String code,String openId,String platform){
        Log.e("==mobile",mobile);
        Log.e("==openId",openId);
        Log.e("==platform",platform);
        Log.e("==verifyCode",code);
        OkHttpUtils.post()
                .url(MOBILEBIND)
                .addParams("openId", openId)
                .addParams("platform", platform)
                .addParams("mobile", mobile)
                .addParams("verifyCode", code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("==bind-onError", e.getMessage());

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("==bind-onResponse", response);
                        json(response);
                    }
                });

    }

    private void json(String response) {
        try {
            JSONObject object = new JSONObject(response);
            JSONObject jsonObject = object.optJSONObject("userInfo");
            String userId = jsonObject.optString("id");
            Intent intent = new Intent(context, GoodsKindActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("userId",userId);
            intent.putExtras(bundle);
            startActivity(intent);
            finish();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
